#include <iostream>
#include <unordered_set>

using namespace std;

void strip(string &str, const string &chars="") {
    if (str.empty()) return;

    unordered_set<string> char_set;
    if (chars.empty()) {
        char_set = { " ", "\n", "\t", "\r" };
    } else {
        for (char c : chars) {
            char_set.insert(string{c});
        }
    }

    auto v = string(1, str.front());
    while (char_set.find(v) != char_set.end()) {
        str.erase(str.begin());
        v = string(1, str.front());
    }
    v = string(1 , str.back());
    while (char_set.find(v) != char_set.end()) {
        str.erase(str.end() - 1);
        v = string(1 , str.back());
    }
}

int main(void)
{
	string s1 = "  hello, world !  \t \n  ";
	strip(s1);
    cout << s1 << endl;

    string s2 = "www.example.com";
    strip(s2, "cmowz.");
    cout << s2 << endl;

    string s3 = "This is test";
    strip(s3);
    cout << s3 << endl;

    return 0;
}
